package club.twelveblog.tblog.controller;

import club.twelveblog.tblog.common.enums.ExceptionEnum;
import club.twelveblog.tblog.common.exception.BlogException;
import club.twelveblog.tblog.entity.BlogInfo;
import club.twelveblog.tblog.entity.Comment;
import club.twelveblog.tblog.entity.User;
import club.twelveblog.tblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/tblog/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     * 根据前端传来的user_id,blog_id,以及评论的时间和内容
     * @return
     */
    @PostMapping
    public ResponseEntity<String> addComment(BlogInfo blogInfo, User user, Comment comment){
        try {
            commentService.addComment(blogInfo,user,comment);
            return ResponseEntity.status(HttpStatus.CREATED).body("评论成功");
        }catch (BlogException e){
            throw new BlogException(ExceptionEnum.COMMENT_FAILED);
        }
    }

}
